package com.yckj.activechicken.business.hr.controller;

import com.yckj.activechicken.business.hr.model.HrLeave;
import com.yckj.activechicken.business.hr.model.vo.HrLeaveVo;
import com.yckj.activechicken.business.hr.service.IHrLeaveService;
import com.yckj.activechicken.common.utils.security.SecurityUtils;
import com.yckj.activechicken.framework.web.controller.BaseController;
import com.yckj.activechicken.framework.web.domain.AjaxResult;
import com.yckj.activechicken.framework.web.page.TableDataInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * Title:com.yckj.activechicken.business.hr.controller
 * </p>
 * <p/>
 * <p>
 * Description: 描述【请假管理控制器】
 * </p>
 * <p/>
 * <p>
 * Copyright: Copyright (c) 2019
 * </p>
 * <p/>
 * <p>
 * Company: 英才汇硕信息技术有限公司
 * </p>
 *
 * @author zhoujy
 * @version 1.0
 * @created 2020/5/25 14:59
 */
@RestController
@RequestMapping("/hr/leave")
public class HrLeaveController extends BaseController{
    @Autowired
    private IHrLeaveService hrLeaveService;

    /**
     * 添加请假单信息，保存为草稿
     * @param hrLeave
     * @return
     */
    @PreAuthorize("@ss.hasPermi('hr:leave:addLeave')")
    @RequestMapping("/addLeave")
    public AjaxResult addLeave(@RequestBody HrLeave hrLeave){
        String username = SecurityUtils.getUsername();
        Date newDate = new Date();
        hrLeave.setStatus("未提交");
        hrLeave.setApplyUser(username);
        hrLeave.setCreateBy(username);
        hrLeave.setCreateTime(newDate);
        hrLeave.setUpdateBy(username);
        hrLeave.setUpdateTime(newDate);
        return toAjax(hrLeaveService.addLeave(hrLeave));
    }

    /**
     * 修改请假操作信息
     * @param hrLeave
     * @return
     */
    @PreAuthorize("@ss.hasPermi('hr:leave:modifyLeave')")
    @RequestMapping("/modifyLeave")
    public AjaxResult modifyLeave(@RequestBody HrLeave hrLeave){
        hrLeave.setUpdateBy(SecurityUtils.getUsername());
        hrLeave.setUpdateTime(new Date());
        return toAjax(hrLeaveService.modifyLeave(hrLeave));
    }

    /**
     * 分页查询请假信息列表
     * @param hrLeave
     * @return
     */
    @PreAuthorize("@ss.hasPermi('hr:leave:listLeave')")
    @GetMapping("/listLeave")
    public TableDataInfo listLeave(HrLeave hrLeave){
        startPage();
        List<HrLeave> hrLeaveList = hrLeaveService.queryLeaveList(hrLeave);
        return getDataTable(hrLeaveList);
    }

    /**
     * 添加请假单信息，并发起请假申请
     * @param hrLeave
     * @return
     */
    @PreAuthorize("@ss.hasPermi('hr:leave:addAndApplyLeave')")
    @RequestMapping("/addAndApplyLeave")
    public AjaxResult addAndApplyLeave(@RequestBody HrLeave hrLeave){
        String username = SecurityUtils.getUsername();
        Date newDate = new Date();
        hrLeave.setStatus("审批中");
        hrLeave.setApplyUser(username);
        hrLeave.setApplyTime(newDate);
        hrLeave.setCreateBy(username);
        hrLeave.setCreateTime(newDate);
        hrLeave.setUpdateBy(username);
        hrLeave.setUpdateTime(newDate);
        return toAjax(hrLeaveService.addAndApplyLeave(hrLeave));
    }

    /**
     * 提交请假申请
     * @param hrLeave
     * @return
     */
    @PreAuthorize("@ss.hasPermi('hr:leave:applyLeave')")
    @RequestMapping("/applyLeave")
    public AjaxResult applyLeave(@RequestBody HrLeave hrLeave){
        String username = SecurityUtils.getUsername();
        Date newDate = new Date();
        hrLeave.setStatus("审批中");
        hrLeave.setApplyUser(username);
        hrLeave.setApplyTime(newDate);
        hrLeave.setUpdateBy(username);
        hrLeave.setUpdateTime(newDate);
        return toAjax(hrLeaveService.applyLeave(hrLeave));
    }

    /**
     * 查询待办列表
     * @param hrLeave
     * @return
     */
    @PreAuthorize("@ss.hasPermi('hr:leave:listTodoLeave')")
    @RequestMapping("/listTodoLeave")
    public AjaxResult listTodoLeave(@RequestBody HrLeave hrLeave){
        List<HrLeaveVo> hrLeaveVoList = hrLeaveService.queryTodoLeaveList(hrLeave);
        return AjaxResult.success(hrLeaveVoList);
    }

    /**
     * 审批假单，存在部门审批和人事审批两种情况
     * @param hrLeaveVo
     * @return
     */
    @PreAuthorize("@ss.hasPermi('hr:leave:approvalLeave')")
    @RequestMapping("/approvalLeave")
    public AjaxResult approvalLeave(@RequestBody HrLeaveVo hrLeaveVo){
        return toAjax(hrLeaveService.approvalLeave(hrLeaveVo));
    }

    /**
     * 放弃请假申请
     * @param hrLeaveVo
     * @return
     */
    @PreAuthorize("@ss.hasPermi('hr:leave:giveUpLeave')")
    @RequestMapping("/giveUpLeave")
    public AjaxResult giveUpLeave(@RequestBody HrLeaveVo hrLeaveVo){
        return toAjax(hrLeaveService.giveUpLeave(hrLeaveVo));
    }


    /**
     * 重新发起请假申请
     * @param hrLeaveVo
     * @return
     */
    @PreAuthorize("@ss.hasPermi('hr:leave:modifyAndApplyLeave')")
    @RequestMapping("/modifyAndApplyLeave")
    public AjaxResult modifyAndApplyLeave(@RequestBody HrLeaveVo hrLeaveVo){
        return toAjax(hrLeaveService.modifyAndApplyLeave(hrLeaveVo));
    }


    /**
     * 查询已办任务列表，后续分页改造
     * @param hrLeave
     * @return
     */
    @PreAuthorize("@ss.hasPermi('hr:leave:listHistoryLeave')")
    @RequestMapping("/listHistoryLeave")
    public AjaxResult listHistoryLeave(@RequestBody HrLeave hrLeave){
        List<HrLeaveVo> hrLeaveVoList = hrLeaveService.queryHistoryLeaveList(hrLeave);
        return AjaxResult.success(hrLeaveVoList);
    }
}
